package com.aitaoxue.palx.ui.register;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.db.GoodDBHelper;
import com.aitaoxue.palx.db.InfoDBHelper;

public class GuideChooseActivity extends BaseActivity implements
		OnItemClickListener {

	enum TYPE {
		COURSE, SCHOOL;
	}

	private TYPE type;

	@InjectView(R.id.listview)
	ListView listView;
	private ArrayAdapter<String> adapter;

	private Map<String, String> map;
	private List<String> content;
	private List<String> content2;

	private boolean SecondFlod = false;

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.choose;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_choose;
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void initView() {
		String t = getIntent().getStringExtra("choose");
		switch (t) {
		case "school":
			type = TYPE.SCHOOL;
			map = InfoDBHelper.getAllProvince();
			content = new ArrayList<String>(map.values());
			break;
		case "course":
			type = TYPE.COURSE;
			map = GoodDBHelper.getAllCate();
			content = new ArrayList<String>(map.values());
		default:
			break;
		}

		ArrayList<String> tmp = new ArrayList<String>(
				Arrays.asList(new String[content.size()]));
		int count = tmp.size();

		Collections.copy(tmp, content);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, tmp);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	@Override
	public void initData() {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (SecondFlod) {
			String item = adapter.getItem(position);
			getIntent().putExtra("res", item);

			setResult(RESULT_OK, getIntent());
			finish();
		} else { // 一重境界
			String item = adapter.getItem(position);
			String key = null;

			Iterator<Entry<String, String>> it = map.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, String> entry = it.next();
				String value = entry.getValue();
				if (value != null && value.equals(item)) {
					key = entry.getKey();
					break;
				}
			}

			if (type == TYPE.SCHOOL) {
				content2 = InfoDBHelper.getSchoolsByProvince(key);
			} else {
				content2 = GoodDBHelper.getKindByCate(key);
			}
			SecondFlod = true;
			adapter.clear();
			adapter.addAll(content2);
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onBackPressed() {
		if (SecondFlod) {
			ArrayList<String> tmp = new ArrayList<String>(
					Arrays.asList(new String[content.size()]));
			Collections.copy(tmp, content);
			SecondFlod = false;
			adapter.clear();
			adapter.addAll(tmp);
			adapter.notifyDataSetChanged();
		} else {
			finish();
		}
	}

}
